﻿<Window x:Class="ZOIL.DataBackend.Server.ServerView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:p="http://hci.uni-konstanz.de/permaedia"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:t="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:local="clr-namespace:ZOIL.DataBackend.Server"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:e="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4" Title="ServerView" Height="800" Width="1200">
    <Window.Background>
        <SolidColorBrush Color="#404040"/>
    </Window.Background>
    <Window.Resources>
        <DataTemplate x:Key="StatusHeaderTemplate">
            <DockPanel>
            <TextBlock Text="Status" DockPanel.Dock="Left"/>
            <TextBlock DockPanel.Dock="Right" Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Content}" Margin="50,0,0,0"></TextBlock>
            </DockPanel>
        </DataTemplate>
        
        <Style x:Key="AddButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="{StaticResource NormalBrush}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Fill="{StaticResource NormalBrush}" RadiusX="5" RadiusY="5" Stroke="{StaticResource NormalBorderBrush}"></Rectangle>
                            <Path x:Name="PlusSign" Width="10" Height="10" SnapsToDevicePixels="False" Stroke="{StaticResource GlyphBrush}" StrokeThickness="3" RenderTransformOrigin="0.5,0.5">
                                <Path.Data>
                                    <GeometryGroup>
                                        <LineGeometry StartPoint="0,5" EndPoint="10,5"></LineGeometry>
                                        <LineGeometry StartPoint="5,0" EndPoint="5,10"></LineGeometry>
                                    </GeometryGroup>
                                </Path.Data>
                            </Path>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="PlusSign" Property="RenderTransform">
                                    <Setter.Value>
                                        <TransformGroup>
                                            <ScaleTransform ScaleX="0.9" ScaleY="0.9"/>
                                            <SkewTransform AngleX="0" AngleY="0"/>
                                            <TranslateTransform X="0" Y="0"/>
                                        </TransformGroup>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="RemoveButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="{StaticResource NormalBrush}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Rectangle Fill="{StaticResource NormalBrush}" RadiusX="5" RadiusY="5" Stroke="{StaticResource NormalBorderBrush}"></Rectangle>
                            <Path x:Name="PlusSign" Width="10" Height="10" SnapsToDevicePixels="False" Stroke="{StaticResource GlyphBrush}" StrokeThickness="3" RenderTransformOrigin="0.5,0.5">
                                <Path.Data>
                                    <GeometryGroup>
                                        <LineGeometry StartPoint="0,5" EndPoint="10,5"></LineGeometry>
                                    </GeometryGroup>
                                </Path.Data>
                            </Path>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="PlusSign" Property="RenderTransform">
                                    <Setter.Value>
                                        <TransformGroup>
                                            <ScaleTransform ScaleX="0.9" ScaleY="0.9"/>
                                            <SkewTransform AngleX="0" AngleY="0"/>
                                            <TranslateTransform X="0" Y="0"/>
                                        </TransformGroup>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>


        <local:DummyConverter x:Key="dummyConverter"></local:DummyConverter>
        <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">

                        <Border Name="Bd"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="0"
                            Padding="0"
                            SnapsToDevicePixels="true"
                            >
                            <ContentPresenter HorizontalAlignment="Stretch"
                                          VerticalAlignment="Stretch"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected"
                                 Value="true">
                                <Setter TargetName="Bd"
                                    Property="Background"
                                    Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                <Setter Property="Foreground"
                                    Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected"
                                           Value="true"/>
                                    <Condition Property="Selector.IsSelectionActive"
                                           Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Bd"
                                    Property="Background"
                                    Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                                <Setter Property="Foreground"
                                    Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled"
                                 Value="false">
                                <Setter Property="Foreground"
                                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <DataTemplate x:Key="ListBoxItemViewTemplate">
            <TextBlock Text="{Binding Name}">
                <i:Interaction.Triggers>
                    <local:RoutedEventTrigger RoutedEvent="UIElement.MouseLeftButtonDown">
                        <e:EventToCommand Command="{Binding Path=EditDatabaseNameCommand, Mode=OneWay}" />
                    </local:RoutedEventTrigger>
                </i:Interaction.Triggers>
            </TextBlock>
        </DataTemplate>
        <DataTemplate x:Key="ListBoxItemEditTemplate">
            <TextBox Text="{Binding Name}" Focusable="True" BorderThickness="0.0" HorizontalAlignment="Stretch" Background="Orange">
                <i:Interaction.Triggers>
                    <local:RoutedEventTrigger RoutedEvent="UIElement.LostKeyboardFocus">
                        <e:EventToCommand Command="{Binding Path=CancelledEditDatabaseNameCommand, Mode=OneWay}" />
                    </local:RoutedEventTrigger>
                </i:Interaction.Triggers>
            </TextBox>
        </DataTemplate>
        <DataTemplate x:Key="ListBoxItemTemplate">
            <ContentControl x:Name="CharacterHost" Focusable="False" Content="{Binding}" ContentTemplate="{StaticResource ListBoxItemViewTemplate}" />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding IsInEditMode}" Value="True">
                    <Setter TargetName="CharacterHost" Property="ContentTemplate" Value="{StaticResource ListBoxItemEditTemplate}" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <Style x:Key="EditStyle" TargetType="Label">
            <Setter Property="BorderThickness" Value="2"></Setter>
            <Setter Property="BorderBrush">
                <Setter.Value>
                    <SolidColorBrush Color="Green"></SolidColorBrush>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding DatabaseFile}" Value="test.yap">
                    <Setter Property="BorderBrush">
                        <Setter.Value>
                            <SolidColorBrush Color="Red"></SolidColorBrush>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>
    <DockPanel>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.6*"></RowDefinition>
                <RowDefinition Height="5"></RowDefinition>
                <RowDefinition Height="0.3*"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150"></ColumnDefinition>
                <ColumnDefinition Width="5"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Expander Margin="0,10,0,0" DockPanel.Dock="Top" Header="Databases" IsExpanded="True">
                <DockPanel>
                    <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" Height="20">
                        <Button Width="20" Height="20" Style="{StaticResource AddButtonStyle}" Foreground="Green" Background="White">
                            <i:Interaction.Triggers>
                                <local:RoutedEventTrigger RoutedEvent="Button.Click">
                                    <e:EventToCommand Command="{Binding Path=AddDatabaseCommand, Mode=OneWay}" />
                                </local:RoutedEventTrigger>
                            </i:Interaction.Triggers>
                        </Button>
                        <Button Width="20" Height="20" Command="{Binding Path=DeleteDatabaseCommand}" Style="{StaticResource RemoveButtonStyle}" Foreground="Red" Background="White">
                            <i:Interaction.Triggers>
                                <local:RoutedEventTrigger RoutedEvent="Button.Click">
                                    <e:EventToCommand Command="{Binding Path=DeleteDatabaseCommand, Mode=OneWay}" />
                                </local:RoutedEventTrigger>
                            </i:Interaction.Triggers>
                        </Button>
                        <!--<Button Command="{Binding DeleteDatabaseCommand}" CommandParameter="{Binding ElementName=DatabasesView}">Delete Database</Button>-->
                    </StackPanel>
                    <ListBox VerticalAlignment="Stretch" x:Name="lbDatabases" IsSynchronizedWithCurrentItem="True" ItemContainerStyle="{StaticResource ListBoxItemStyle}" ItemTemplate="{StaticResource ListBoxItemTemplate}" ItemsSource="{Binding Path=CVSDatabasesView}"></ListBox>
                </DockPanel>
            </Expander>
            <GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Transparent"></GridSplitter>
            <ScrollViewer Grid.Column="2" x:Name="svDetails">
                <DockPanel DataContext="{Binding CVSDatabasesView.CurrentItem}" Margin="10,0,20,0">
                    <TextBlock Text="{Binding Name}" DockPanel.Dock="Top" FontSize="24" HorizontalAlignment="Center" Foreground="{StaticResource ForegroundBrush}"></TextBlock>
                    <StackPanel Margin="0,10,0,0" HorizontalAlignment="Stretch">
                        <Expander Margin="0,10,0,0" IsExpanded="True" Header="{Binding Status}" HeaderTemplate="{StaticResource StatusHeaderTemplate}">
                            <StackPanel>
                                <StackPanel Margin="20,0,0,0" Orientation="Horizontal">
                                    <CheckBox VerticalAlignment="Center" IsChecked="{Binding UseMemoryIOAdapter}" IsEnabled="{Binding Status, Converter={local:StatusToBooleanConverter}}">Run Database in RAM</CheckBox>
                                    <Button Margin="20,0,0,0" Content="Start Database">
                                        <i:Interaction.Triggers>
                                            <local:RoutedEventTrigger RoutedEvent="Button.Click">
                                                <e:EventToCommand Command="{Binding Path=StartDatabaseCommand, Mode=OneWay}" />
                                            </local:RoutedEventTrigger>
                                        </i:Interaction.Triggers>
                                    </Button>
                                    <Button Margin="20,0,0,0" Content="Stop Database">
                                        <i:Interaction.Triggers>
                                            <local:RoutedEventTrigger RoutedEvent="Button.Click">
                                                <e:EventToCommand Command="{Binding Path=StopDatabaseCommand, Mode=OneWay}" />
                                            </local:RoutedEventTrigger>
                                        </i:Interaction.Triggers>
                                    </Button>
                                    <Button Margin="20,0,0,0" Content="Clear Database">
                                        <i:Interaction.Triggers>
                                            <local:RoutedEventTrigger RoutedEvent="Button.Click">
                                                <e:EventToCommand Command="{Binding Path=ClearDatabaseCommand, Mode=OneWay}" />
                                            </local:RoutedEventTrigger>
                                        </i:Interaction.Triggers>
                                    </Button>
                                </StackPanel>
                                <Grid Margin="20,20,0,0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="50"></RowDefinition>
                                        <RowDefinition Height="50"></RowDefinition>
                                        <RowDefinition Height="50"></RowDefinition>
                                        <RowDefinition Height="100"></RowDefinition>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="120"></ColumnDefinition>
                                        <ColumnDefinition Width="200"></ColumnDefinition>
                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
    
                                    <!-- Row 0 -->
                                    <TextBlock VerticalAlignment="Center">Path:</TextBlock>
                                    <TextBox Grid.Column="1" VerticalAlignment="Center" Text="{Binding DatabaseFile, UpdateSourceTrigger=PropertyChanged}"></TextBox>
                                    <TextBlock Grid.Column="2" VerticalAlignment="Center" Margin="20,0,0,0">
                                        <TextBlock.Text>
                                            <MultiBinding StringFormat="{}{0}\{1}.db4o">
                                                <Binding ElementName="svDetails" Path="DataContext.Model.DatabaseDirectory"/>
                                                <Binding Path="DatabaseFile"/>
                                            </MultiBinding>
                                        </TextBlock.Text>
                                    </TextBlock>

                                    <!-- Row 1 -->
                                    <TextBlock VerticalAlignment="Center" Grid.Row="1">Port:</TextBlock>
                                    <TextBox VerticalAlignment="Center" Grid.Row="1" Grid.Column="1" Text="{Binding Port}" />
                                    
                                    <!-- Row 2 -->
                                    <TextBlock VerticalAlignment="Center" Grid.Row="2">Database Directory:</TextBlock>
                                    <TextBox VerticalAlignment="Center" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2" Text="{Binding ElementName=svDetails, Path=DataContext.Model.DatabaseDirectory}" />
                                    
                                    <!-- Row 3 -->
                                    <TextBlock VerticalAlignment="Top" Margin="0,20,0,0" Grid.Row="3">Description:</TextBlock>
                                    <TextBox Margin="0,20,0,0" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" VerticalAlignment="Center" Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding Description}" Height="80" />
                                </Grid>
                            </StackPanel>
                        </Expander>
                        <Expander Margin="0,10,0,0" IsExpanded="True" Header="Benutzer">
                            <StackPanel Margin="0,10,0,0">
                                <StackPanel Orientation="Horizontal" Margin="20,0,0,0">
                                    <Button Command="{Binding AddUserCommand}">Add User</Button>
                                    <Button Margin="20,0,0,0" Command="{Binding RemoveUserCommand}">Remove User</Button>
                                </StackPanel>
                                <Border Margin="0,10,0,0">
                                    <t:DataGrid Height="100" Width="200" HeadersVisibility="Column" AutoGenerateColumns="False" Margin="20,0,0,0" HorizontalAlignment="Left" ItemsSource="{Binding Users}">
                                        <t:DataGrid.Columns>
                                            <t:DataGridTextColumn Width="100" Header="Username" Binding="{Binding Username}"></t:DataGridTextColumn>
                                            <t:DataGridTextColumn Width="100" Header="Password" Binding="{Binding Password}"></t:DataGridTextColumn>
                                        </t:DataGrid.Columns>
                                    </t:DataGrid>
                                </Border>
                            </StackPanel>
                        </Expander>
                        <Expander Margin="0,10,0,0" Header="Aktionen">
                            
                        </Expander>
                    </StackPanel>
                </DockPanel>
            </ScrollViewer>
            <GridSplitter Grid.Row="1" Background="Transparent" ResizeDirection="Rows" HorizontalAlignment="Stretch" Grid.ColumnSpan="3"></GridSplitter>
            <TextBox DataContext="{Binding CVSDatabasesView.CurrentItem}" Text="{Binding LogText}" Grid.Row="2" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" AcceptsReturn="True" Grid.ColumnSpan="3"></TextBox>
        </Grid>
    </DockPanel>
</Window>
